
/**
* @Title: ShiroSessionManager.java
* @Package com.cenntro.auth.shiro
* @Description: 
* @author Administrator
* @date 2019年7月8日
* @version V1.0
*/
package com.cenntro.auth.shiro;

import java.io.Serializable;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionException;
import org.apache.shiro.session.mgt.SessionKey;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;

import lombok.extern.slf4j.Slf4j;

/**
* @ClassName: ShiroSessionManager
* @Description: TODO
* @author Administrator
* @date 2019年7月8日
*
*/
@Slf4j
public class ShiroSessionManager extends DefaultWebSessionManager{
    

    
    /**
     * get access token
     *
     * @param request
     * @return
     */
    private String getAccessToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        return token != null ? token : "";
    }
    /** (非 Javadoc)
    * <p>Title: getSessionId</p>
    * <p>Description: </p>
    * @param request
    * @param response
    * @return
    * @see org.apache.shiro.web.session.mgt.DefaultWebSessionManager#getSessionId(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
    */
//    @Override
//    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
//        // TODO Auto-generated method stub
//        return super.getSessionId(request, response);
//    }
 
    @Override
    protected Serializable  getSessionId(ServletRequest request, ServletResponse response) {
        Serializable id = super.getSessionId(request, response);
        if (null == id/* && WebUtils.isWeb(sessionKey)*/) {
            String token = getAccessToken((HttpServletRequest)request);
            if(StringUtils.isBlank(token)){
                token = request.getParameter("token");
            }
            if (StringUtils.isNotBlank(token)) {
                Session s = super.sessionDAO.readSession(token);  //cache 类添加前缀 shiro_session
                if(s != null){
                    id = s.getId();
                }
            }
        }
        return id;
    }
    

    
    /** (非 Javadoc)
    * <p>Title: getSession</p>
    * <p>Description: </p>
    * @param key
    * @return
    * @throws SessionException
    * @see org.apache.shiro.session.mgt.AbstractNativeSessionManager#getSession(org.apache.shiro.session.mgt.SessionKey)
    */
    @Override
    public Session getSession(SessionKey key) throws SessionException {
        // TODO Auto-generated method stub
        Session s= super.getSession(key);
        if(s != null){
            
            log.info(s.toString());
        }else {
//            ServletRequest ServletRequest = WebUtils.getRequest(key);
//            ServletRequest.getParameter("token");
        }
        return s;
    }
}
